/*

 exercise 1.1 - voltmeter
 
 Created 07/04/2010 --- By John Boxall --- http://tronixstuff.wordpress.com --- CC by-sa v3.0 Share the love!
 Updated 17/06/2010 - also sends the voltage to the serial monitor
 
 Measures voltage between 0~10V DC that is fed into an analogue pin via a voltage divider circuit, and displays it using 10 LEDs
 
 */

int voltage = 0; // for actual voltage

int i = 0; // for loops
float v2 = 0; // for voltage calculation

int del = 10; // for delays between LEDs turning on and off

void setup()

{

  // initialize the digital pins as outputs, no need to initialise input analogue pin

  for (i = 2; i<=11 ; i++)

  {

    pinMode(i, OUTPUT);

  } // end of for loop

  Serial.begin(9600); // open the serial port to send data back to the computer at 9600 bps

} // end of setup

void loop()

{

  voltage = analogRead(5); // get reading from analogue pin 5
  
  // now we need to convert the raw value from analogRead() to an actual voltage
  // we know that our actual voltage range is 0~10 volts, and the analogRead() returns a value 0~1023
  // so, the actual voltage will be whatever analogRead() returns divided by 102.3
  
  v2 = voltage/102.3; // Converts analogRead() value
  Serial.print("Voltage: ");
  Serial.println(v2, 2); // rounds v2 to two decimal places

    if (voltage <=10)

  {

    delay(del);

  }

  else if (voltage <= 102)

  {

    digitalWrite(2,HIGH);

    delay (del);

    digitalWrite(2,LOW);

  }

  else if (voltage <=205)

  {

    digitalWrite(3,HIGH);

    delay (del);

    digitalWrite(3,LOW);

  }

  else if (voltage <=307)

  {

    digitalWrite(4,HIGH);

    delay (del);

    digitalWrite(4,LOW);

  }

  else if (voltage <= 409)

  {

    digitalWrite(5,HIGH);

    delay(del);

    digitalWrite(5,LOW);

  }

  else if (voltage <=511)

  {

    digitalWrite(6,HIGH);

    delay(del);

    digitalWrite(6,LOW);

  }

  else if (voltage <=614)

  {

    digitalWrite(7,HIGH);

    delay(del);

    digitalWrite(7,LOW);

  }

  else if (voltage <= 716)

  {

    digitalWrite(8,HIGH);

    delay(del);

    digitalWrite(8,LOW);

  }

  else if (voltage <=818)

  {

    digitalWrite(9, HIGH);

    delay(del);

    digitalWrite(9, LOW);

  }

  else if (voltage <=921)

  {

    digitalWrite(10,HIGH);

    delay(del);

    digitalWrite(10,LOW);

  }

  else if (voltage <=1023)

  {

    digitalWrite(11,HIGH);

    delay(del);

    digitalWrite(11, LOW);

  }

}

